using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;

using Krystalware.SlickUpload;
using Krystalware.SlickUpload.Status;

using Adge.Common;
using System.IO;
using Adge.Common.Config;

namespace Adge.Web.UI.Admin
{
    public partial class CoverManager : System.Web.UI.Page
    {
        private log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        
        protected void Page_Load(object sender, EventArgs e)
        {            
            if(!Page.IsPostBack)
            {
                ListCovers();
            }
        }

        private void ListCovers()
        {
            CoverConfig cover = new CoverConfig();
            repFiles.DataSource = cover.GetImages();
            repFiles.DataBind();
        }

        void ThumbnailCover()
        {            
            Thumbnail thumb = new Thumbnail();            
            UploadStatus status = HttpUploadModule.GetUploadStatus();
            string fileFullName = status.GetUploadedFiles()[0].LocationInfo["fileName"];
            string fileName = Path.GetFileName(fileFullName);
            string thumbnailPath = AdgeConfig.CoverImageSoragePhyPath + fileName;
            thumb.MakeThumbnail(fileFullName, thumbnailPath, 180, 240, ThumbMode.Cut);
            SaveImageProfile(thumbnailPath, fileFullName);
        }

        private void SaveImageProfile(string thumbnailPath, string fileFullName)
        {
            using (StreamWriter sw = File.CreateText(Path.ChangeExtension(thumbnailPath, ".txt")))
            {
                fileFullName = fileFullName.Substring(AdgeConfig.FileSoragePhyPath.Length);
                sw.WriteLine(fileFullName);
                sw.Flush();
            }
        }

        private string GetImageProfile(string thumbnailPath)
        {
            string fileFullName = string.Empty;
            using (StreamReader sr = File.OpenText(Path.ChangeExtension(thumbnailPath, ".txt")))
            {
                fileFullName = sr.ReadLine();
            }
            fileFullName = AdgeConfig.FileSoragePhyPath + fileFullName;
            return fileFullName;
        }

        private void DeleteImageProfile(string thumbnailPath)
        {
            File.Delete(Path.ChangeExtension(thumbnailPath, ".txt"));
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            try
            {
                ThumbnailCover();
            }
            catch (Exception ex)
            {
                lblStatus.Text = ex.Message;                
            }
            ListCovers();
        }

        protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            //reset index
            repFiles.CurrentPageIndex = e.NewPageIndex;
            ListCovers();
        }

        protected void repFiles_DeleteCommand(object source, DataListCommandEventArgs e)
        {
            string fileName = e.CommandArgument.ToString();
            //Response.Write(filename);
            string thumbnailPath = AdgeConfig.CoverImageSoragePhyPath + fileName;
            try
            {
                File.Delete(GetImageProfile(thumbnailPath));
                File.Delete(thumbnailPath);
                DeleteImageProfile(thumbnailPath);
            }
            catch (Exception ex)
            {
                lblStatus.Text = ex.Message;
            }
            
            ListCovers();
        }
    }
}
